package com.tomas.demo.config;

import com.tomas.demo.common.CommonInterceptor;
import com.tomas.demo.common.RateLimitInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.velocity.VelocityAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;

import java.nio.charset.Charset;
import java.util.List;

@Configuration
@ComponentScan(basePackages = {"com.tomas.demo.common", "com.tomas.demo.controller"})
@EnableWebMvc
@EnableAutoConfiguration(exclude = {VelocityAutoConfiguration.class })
public class WebAppConfig extends WebMvcConfigurerAdapter{

    @Autowired
    private CommonInterceptor commonInterceptor;
    @Autowired
    private RateLimitInterceptor rateLimitInterceptor;

    public void addInterceptors(InterceptorRegistry registry) {
       //通用拦截器 拦截打印请求和耗时
       registry.addInterceptor(commonInterceptor);
       //防刷拦截器(开了响应时间 5ms --> 200ms 可能是因为网络的原因)
      //registry.addInterceptor(rateLimitInterceptor);

    }
    @Bean
    public InternalResourceViewResolver DefalutViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/views/");
        resolver.setSuffix(".jsp");
        resolver.setOrder(3);
        return resolver;
    }
    @Bean
    public FreeMarkerViewResolver FreeMarkerViewResolver() {
        FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
        resolver.setPrefix("/templates");
       // resolver.setSuffix(".ftl");
        resolver.setOrder(2);
        return resolver;
    }
    @Bean
    public VelocityViewResolver VelocityViewResolver() {
        VelocityViewResolver resolver = new VelocityViewResolver();
        resolver.setPrefix("/templates");
        resolver.setContentType("text/html;charset=UTF-8");
       // resolver.setSuffix(".vm");
        resolver.setOrder(1);
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/templates/");
    }

    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //responseBody中文乱码 因为默认的相应编码"ISO-8859-1"
        converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
    }
}